package net.jiupinshi.controller.store;

import net.jiupinshi.common.Result;
import net.jiupinshi.customAnnotation.LoginAnnotation;
import net.jiupinshi.dto.StoreSimpleDTO;
import net.jiupinshi.dto.UserDTO;
import net.jiupinshi.entity.Store;
import net.jiupinshi.entity.User;
import net.jiupinshi.service.store.StoreService;
import net.jiupinshi.util.SessionUtil;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Despription: 店铺控制层
 * @Author: penghui.guo
 * @Date: 2018/10/5 16:10
 */
@RestController
@RequestMapping("store")
public class StoreController {

    @Autowired
    StoreService storeService;

    @LoginAnnotation
    @RequestMapping("insertStore")
    public Result insertStore(HttpServletRequest request, Store store){
        Result result = new Result();
        UserDTO user = SessionUtil.getUser(request);
        store.setUserId(user.getId());
        store.setUserName(user.getName());
        int i = storeService.insertStore(store);
        if(i == 1){
            result.setSuccess();
        }
        return result;

    }

    @LoginAnnotation
    @RequestMapping("updateStore")
    public Result updateStoreById(Store store){
        Result result = new Result();
        int i = storeService.updateStoreById(store);
        if(i > 0){
            result.setSuccess();
        }
        return result;
    }

    @LoginAnnotation
    @RequestMapping("getStores")
    public Result getStores(){
        Result<List<StoreSimpleDTO>> result = new Result();
        List<StoreSimpleDTO> storeList = storeService.getStores();
        result.setSuccess();
        result.setData(storeList);
        return result;
    }

}
